﻿<%@ WebHandler Language="C#" Class="SIPO.Web.AjaxHandler.Loaddata.getdebtor" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.Enum;
using SIPO.Web.Support;
using Newtonsoft.Json.Linq;

namespace SIPO.Web.AjaxHandler.Loaddata
{
    public class getdebtor : SIPO.Web.BaseHandler
    {
        recDebtorRepo recDebtorDal = new recDebtorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        string MethodName = string.Empty;
        string subsubsectors = string.Empty;
        public override void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);

            if (context.Request.Params["method"] != null)
                MethodName = context.Request.Params["method"];

            if (MethodName == "getbysubsubsector")
            {
                if (context.Request.Params["subsubsectors"] != null)
                    subsubsectors = context.Request.Params["subsubsectors"];

                List<recDebtor> listrecDebtor = recDebtorDal.GetList(subsubsectors);
                var result = listrecDebtor;
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }

            else if (MethodName == "getbyid")
            {
                int debtorID = Convert.ToInt32(context.Request.Params["debtorID"]);
                List<recDebtor> listrecDebtor = recDebtorDal.GetListById(debtorID);
                var result = listrecDebtor;
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }
            else if (MethodName == "getbycorporateactiontype")
            {
                string typeId = (!string.IsNullOrEmpty(context.Request.Params["CorporateActionTypeID"]) ? context.Request.Params["CorporateActionTypeID"] : string.Empty);
                List<recDebtor> debtors = recDebtorDal.GetListByCorporateAction("2", "4, 5");

                List<recDebtor> debtorsFiltered = null;
                
                if (typeId.Equals("1") || typeId.Equals("2")) // Penggabungan / Peleburan Perusahaan || Pengambilalihan Perusahaan Terbuka
                {
                    if (base.DebtorID.HasValue)
                    {
                        debtorsFiltered = debtors.FindAll(x => !(x.DebtorID.Equals(base.DebtorID.Value)));
                    }
                    else
                        debtorsFiltered = debtors;
                    
                }
                else  // => Perubahan Perusahaan Terbuka Menjadi Perusahaan Tertutup || Penambahan Modal Tanpa Hak Memesan Efek Terlebih Dahulu
                {
                    if (base.DebtorID.HasValue)
                    {
                        debtorsFiltered = debtors.FindAll(x => (x.DebtorID.Equals(base.DebtorID.Value)));
                    }
                    else
                        debtorsFiltered = debtors;
                    
                }

                if (debtorsFiltered == null)
                    debtorsFiltered = new List<recDebtor>(); // New List<> for not throw object null. 

                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(debtorsFiltered));

            }

        }
        public override bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}